home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / URL.java < prev    next >
Text File  |  1998-09-22  |  26KB  |  711 lines

  1. /*
  2.  * @(#)URL.java    1.44 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.Hashtable;
  21. import java.util.StringTokenizer;
  22.  
  23. /**
  24.  * Class <code>URL</code> represents a Uniform Resource 
  25.  * Locator, a pointer to a "resource" on the World 
  26.  * Wide Web. A resource can be something as simple as a file or a 
  27.  * directory, or it can be a reference to a more complicated object, 
  28.  * such as a query to a database or to a search engine. More 
  29.  * information on the types of URLs and their formats can be found at:
  30.  * <ul><code>
  31.  *     http://www.ncsa.uiuc.edu/demoweb/url-primer.html
  32.  * </code></ul>
  33.  * <p>
  34.  * In general, a URL can be broken into several parts. The previous 
  35.  * example of a URL indicates that the protocol to use is 
  36.  * <code>http</code> (HyperText Transport Protocol) and that the 
  37.  * information resides on a host machine named 
  38.  * <code>www.ncsa.uiuc.edu</code>. The information on that host 
  39.  * machine is named <code>demoweb/url-primer.html</code>. The exact 
  40.  * meaning of this name on the host machine is both protocol 
  41.  * dependent and host dependent. The information normally resides in 
  42.  * a file, but it could be generated on the fly. This component of 
  43.  * the URL is called the <i>file</i> component, even though the 
  44.  * information is not necessarily in a file. 
  45.  * <p>
  46.  * A URL can optionally specify a "port", which is the 
  47.  * port number to which the TCP connection is made on the remote host 
  48.  * machine. If the port is not specified, the default port for 
  49.  * the protocol is used instead. For example, the default port for 
  50.  * <code>http</code> is <code>80</code>. An alternative port could be 
  51.  * specified as: 
  52.  * <ul><code>
  53.  *     http://www.ncsa.uiuc.edu:8080/demoweb/url-primer.html
  54.  * </code></ul>
  55.  * <p>
  56.  * A URL may have appended to it an "anchor", also known 
  57.  * as a "ref" or a "reference". The anchor is 
  58.  * indicated by the sharp sign character "#" followed by 
  59.  * more characters. For example, 
  60.  * <ul><code>
  61.  *     http://java.sun.com/index.html#chapter1
  62.  * </code></ul>
  63.  * <p>
  64.  * This anchor is not technically part of the URL. Rather, it 
  65.  * indicates that after the specified resource is retrieved, the 
  66.  * application is specifically interested in that part of the 
  67.  * document that has the tag <code>chapter1</code> attached to it. The 
  68.  * meaning of a tag is resource specific. 
  69.  * <p>
  70.  * An application can also specify a "relative URL", 
  71.  * which contains only enough information to reach the resource 
  72.  * relative to another URL. Relative URLs are frequently used within 
  73.  * HTML pages. For example, if the contents of the URL:
  74.  * <ul><code>
  75.  *     http://java.sun.com/index.html
  76.  * </code></ul>
  77.  * contained within it the relative URL:
  78.  * <ul><code>
  79.  *     FAQ.html
  80.  * </code></ul>
  81.  * it would be a shorthand for:
  82.  * <ul><code>
  83.  *     http://java.sun.com/FAQ.html
  84.  * </code></ul>
  85.  * <p>
  86.  * The relative URL need not specify all the components of a URL. If 
  87.  * the protocol, host name, or port number is missing, the value is 
  88.  * inherited from the fully specified URL. The file component must be 
  89.  * specified. The optional anchor is not inherited. 
  90.  *
  91.  * @author  James Gosling
  92.  * @version 1.44, 07/01/98
  93.  * @since   JDK1.0
  94.  */
  95. public final class URL implements java.io.Serializable {
  96.     /**
  97.      * The property which specifies the package prefix list to be scanned
  98.      * for protocol handlers.  The value of this property (if any) should
  99.      * be a vertical bar delimited list of package names to search through
  100.      * for a protocol handler to load.  The policy of this class is that
  101.      * all protocol handlers will be in a class called <protocolname>.Handler,
  102.      * and each package in the list is examined in turn for a matching
  103.      * handler.  If none are found (or the property is not specified), the
  104.      * default package prefix, sun.net.www.protocol, is used.  The search
  105.      * proceeds from the first package in the last to the last and stops
  106.      * when a match is found.
  107.      */
  108.     private static final String protocolPathProp = "java.protocol.handler.pkgs";
  109.  
  110.     /** 
  111.      * The protocol to use (ftp, http, nntp, ... etc.) . 
  112.      */
  113.     private String protocol;
  114.  
  115.     /** 
  116.      * The host name in which to connect to. 
  117.      */
  118.     private String host;
  119.  
  120.     /** 
  121.      * The protocol port to connect to. 
  122.      */
  123.     private int port = -1;
  124.  
  125.     /** 
  126.      * The specified file name on that host. 
  127.      */
  128.     private String file;
  129.  
  130.     /** 
  131.      * # reference. 
  132.      */
  133.     private String ref;
  134.  
  135.     /**
  136.      * The URLStreamHandler for this URL.
  137.      */
  138.     transient URLStreamHandler handler;
  139.  
  140.     /* Our hash code */
  141.     private int hashCode = -1;
  142.  
  143.     /** 
  144.      * Creates a <code>URL</code> object from the specified 
  145.      * <code>protocol</code>, <code>host</code>, <code>port</code> 
  146.      * number, and <code>file</code>. Specifying a <code>port</code> 
  147.      * number of <code>-1</code> indicates that the URL should use 
  148.      * the default port for the protocol. 
  149.      * <p>
  150.      * If this is the first URL object being created with the specified 
  151.      * protocol, a <i>stream protocol handler</i> object, an instance of 
  152.      * class <code>URLStreamHandler</code>, is created for that protocol:
  153.      * <ol>
  154.      * <li>If the application has previously set up an instance of
  155.      *     <code>URLStreamHandlerFactory</code> as the stream handler factory,
  156.      *     then the <code>createURLStreamHandler</code> method of that instance
  157.      *     is called with the protocol string as an argument to create the
  158.      *     stream protocol handler.
  159.      * <li>If no <code>URLStreamHandlerFactory</code> has yet been set up,
  160.      *     or if the factory's <code>createURLStreamHandler</code> method
  161.      *     returns <code>null</code>, then the constructor finds the 
  162.      *     value of the system property:
  163.      *     <ul><code>
  164.      *         java.handler.protol.pkgs
  165.      *     </code></ul>
  166.      *     If the value of that system property is not <code>null</code>,
  167.      *     it is interpreted as a list of packages separated by a vertical
  168.      *     slash character '<code>|</code>'. The constructor tries to load 
  169.      *     the class named:
  170.      *     <ul><code>
  171.      *         <<i>package</i>>.<<i>protocol</i>>.Handler
  172.      *     </code></ul>
  173.      *     where <<i>package</i>> is replaced by the name of the package
  174.      *     and <<i>protocol</i>> is replaced by the name of the protocol.
  175.      *     If this class does not exist, or if the class exists but it is not
  176.      *     a subclass of <code>URLStreamHandler</code>, then the next package
  177.      *     in the list is tried.
  178.      * <li>If the previous step fails to find a protocol handler, then the
  179.      *     constructor tries to load the class named:
  180.      *     <ul><code>
  181.      *         sun.net.www.protocol.<<i>protocol</i>>.Handler
  182.      *     </code></ul>
  183.      *     If this class does not exist, or if the class exists but it is not a
  184.      *     subclass of <code>URLStreamHandler</code>, then a
  185.      *     <code>MalformedURLException</code> is thrown.
  186.      * </ol>
  187.      *
  188.      * @param      protocol   the name of the protocol.
  189.      * @param      host       the name of the host.
  190.      * @param      port       the port number.
  191.      * @param      file       the host file.
  192.      * @exception  MalformedURLException  if an unknown protocol is specified.
  193.      * @see        java.lang.System#getProperty(java.lang.String)
  194.      * @see        java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)
  195.      * @see        java.net.URLStreamHandler
  196.      * @see        java.net.URLStreamHandlerFactory#createURLStreamHandler(java.lang.String)
  197.      * @since      JDK1.0
  198.      */
  199.     public URL(String protocol, String host, int port, String file)
  200.     throws MalformedURLException {
  201.     this.protocol = protocol;
  202.     this.host = host;
  203.     this.port = port;
  204.     int ind = file.indexOf('#');
  205.     this.file = ind < 0 ? file: file.substring(0, ind);
  206.     this.ref = ind < 0 ? null: file.substring(ind + 1);
  207.     if ((handler = getURLStreamHandler(protocol)) == null) {
  208.         throw new MalformedURLException("unknown protocol: " + protocol);
  209.     }
  210.     }
  211.  
  212.     /** 
  213.      * Creates an absolute URL from the specified <code>protocol</code> 
  214.      * name, <code>host</code> name, and <code>file</code> name. The 
  215.      * default port for the specified protocol is used. 
  216.      * <p>
  217.      * This method is equivalent to calling the four-argument 
  218.      * constructor with the arguments being <code>protocol</code>, 
  219.      * <code>host</code>, <code>-1</code>, and <code>file</code>. 
  220.      *
  221.      * @param      protocol   the protocol to use.
  222.      * @param      host       the host to connect to.
  223.      * @param      file       the file on that host.
  224.      * @exception  MalformedURLException  if an unknown protocol is specified.
  225.      * @see        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  226.      * @since      JDK1.0
  227.      */
  228.     public URL(String protocol, String host, String file) throws MalformedURLException {
  229.     this(protocol, host, -1, file);
  230.     }
  231.  
  232.     /**
  233.      * Creates a <code>URL</code> object from the <code>String</code> 
  234.      * representation. 
  235.      * <p>
  236.      * This constructor is equivalent to a call to the two-argument 
  237.      * constructor with a <code>null</code> first argument. 
  238.      *
  239.      * @param      spec   the <code>String</code> to parse as a URL.
  240.      * @exception  MalformedURLException  If the string specifies an
  241.      *               unknown protocol.
  242.      * @see        java.net.URL#URL(java.net.URL, java.lang.String)
  243.      * @since      JDK1.0
  244.      */
  245.     public URL(String spec) throws MalformedURLException {
  246.     this(null, spec);
  247.     }
  248.  
  249.     /** 
  250.      * Creates a URL by parsing the specification <code>spec</code> 
  251.      * within a specified context. If the <code>context</code> argument 
  252.      * is not <code>null</code> and the <code>spec</code> argument is a 
  253.      * partial URL specification, then any of the strings missing 
  254.      * components are inherited from the <code>context</code> argument. 
  255.      * <p>
  256.      * The specification given by the <code>String</code> argument is 
  257.      * parsed to determine if it specifies a protocol. If the 
  258.      * <code>String</code> contains an ASCII colon '<code>:</code>'
  259.      * character before the first occurrence of an ASCII slash character 
  260.      * '<code>/</code>', then the characters before the colon comprise 
  261.      * the protocol. 
  262.      * <ul>
  263.      * <li>If the <code>spec</code> argument does not specify a protocol:
  264.      *     <ul>
  265.      *     <li>If the context argument is not <code>null</code>, then the
  266.      *         protocol is copied from the context argument.
  267.      *     <li>If the context argument is <code>null</code>, then a
  268.      *         <code>MalformedURLException</code> is thrown.
  269.      *     </ul>
  270.      * <li>If the <code>spec</code> argument does specify a protocol:
  271.      *     <ul>
  272.      *     <li>If the context argument is <code>null</code>, or specifies a
  273.      *         different protocol than the specification argument, the context
  274.      *         argument is ignored.
  275.      *     <li>If the context argument is not <code>null</code> and specifies
  276.      *         the same protocol as the specification, the <code>host</code>,
  277.      *         <code>port</code> number, and <code>file</code> are copied from
  278.      *         the context argument into the newly created <code>URL</code>.
  279.      *     </ul>
  280.      * </ul>
  281.      * <p>
  282.      * The constructor then searches for an appropriate stream protocol 
  283.      * handler of type <code>URLStreamHandler</code> as outlined for:
  284.      * <ul><code>
  285.      *     java.net.URL#URL(java.lang.String, java.lang.String, int,
  286.      *                      java.lang.String)
  287.      * </code></ul>
  288.      * The stream protocol handler's 
  289.      * <code>parseURL</code> method is called to parse the remaining 
  290.      * fields of the specification that override any defaults set by the 
  291.      * context argument. 
  292.  
  293.      * @param      context   the context in which to parse the specification.
  294.      * @param      spec      a <code>String</code> representation of a URL.
  295.      * @exception  MalformedURLException  if no protocol is specified, or an
  296.      *               unknown protocol is found.
  297.      * @see        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  298.      * @see        java.net.URLStreamHandler
  299.      * @see        java.net.URLStreamHandler#parseURL(java.net.URL, java.lang.String, int, int)
  300.      * @since   JDK1.0
  301.      */
  302.     public URL(URL context, String spec) throws MalformedURLException {
  303.     String original = spec;
  304.     int i, limit, c;
  305.     int start = 0;
  306.     String newProtocol = null;
  307.     boolean aRef=false;
  308.  
  309.     try {
  310.         limit = spec.length();
  311.         while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) {
  312.         limit--;    //eliminate trailing whitespace
  313.         }
  314.         while ((start < limit) && (spec.charAt(start) <= ' ')) {
  315.         start++;    // eliminate leading whitespace
  316.         }
  317.  
  318.         if (spec.regionMatches(true, start, "url:", 0, 4)) {
  319.         start += 4;
  320.         }
  321.         if (start < spec.length() && spec.charAt(start) == '#') {
  322.         /* we're assuming this is a ref relative to the context URL.
  323.          * This means protocols cannot start w/ '#', but we must parse
  324.          * ref URL's like: "hello:there" w/ a ':' in them.
  325.          */
  326.         aRef=true;
  327.         }
  328.         for (i = start ; !aRef && (i < limit) && ((c = spec.charAt(i)) != '/') ; i++) {
  329.         if (c == ':') {
  330.             newProtocol = spec.substring(start, i).toLowerCase();
  331.             start = i + 1;
  332.             break;
  333.         }
  334.         }
  335.         // Only use our context if the protocols match.
  336.         if ((context != null) && ((newProtocol == null) ||
  337.                     newProtocol.equals(context.protocol))) {
  338.         protocol = context.protocol;
  339.         host = context.host;
  340.         port = context.port;
  341.         file = context.file;
  342.         } else {
  343.         protocol = newProtocol;
  344.         }
  345.  
  346.         if (protocol == null) {
  347.         throw new MalformedURLException("no protocol: "+original);
  348.         }
  349.  
  350.         if ((handler = getURLStreamHandler(protocol)) == null) {
  351.         throw new MalformedURLException("unknown protocol: "+protocol);
  352.         }
  353.  
  354.         i = spec.indexOf('#', start);
  355.         if (i >= 0) {
  356.         ref = spec.substring(i + 1, limit);
  357.         limit = i;
  358.         }
  359.         handler.parseURL(this, spec, start, limit);
  360.  
  361.     } catch(MalformedURLException e) {
  362.         throw e;
  363.     } catch(Exception e) {
  364.         throw new MalformedURLException(original + ": " + e);
  365.     }
  366.     }
  367.  
  368.     /**
  369.      * Sets the fields of the URL. This is not a public method so that 
  370.      * only URLStreamHandlers can modify URL fields. URLs are 
  371.      * otherwise constant.
  372.      *
  373.      * REMIND: this method will be moved to URLStreamHandler
  374.      *
  375.      * @param protocol the protocol to use
  376.      * @param host the host name to connecto to
  377.      * @param port the protocol port to connect to
  378.      * @param file the specified file name on that host
  379.      * @param ref the reference
  380.      */
  381.     protected void set(String protocol, String host, int port, String file, String ref) {
  382.     this.protocol = protocol;
  383.     this.host = host;
  384.     this.port = port;
  385.     this.file = file;
  386.     this.ref = ref;
  387.     }
  388.  
  389.     /**
  390.      * Returns the port number of this <code>URL</code>.
  391.      * Returns -1 if the port is not set.
  392.      *
  393.      * @return  the port number
  394.      * @since   JDK1.0
  395.      */
  396.     public int getPort() {
  397.     return port;
  398.     }
  399.  
  400.     /**
  401.      * Returns the protocol name this <code>URL</code>.
  402.      *
  403.      * @return  the protocol of this <code>URL</code>.
  404.      * @since   JDK1.0
  405.      */
  406.     public String getProtocol() {
  407.     return protocol;
  408.     }
  409.  
  410.     /**
  411.      * Returns the host name of this <code>URL</code>, if applicable.
  412.      * For "<code>file</code>" protocol, this is an empty string.
  413.      *
  414.      * @return  the host name of this <code>URL</code>.
  415.      * @since   JDK1.0
  416.      */
  417.     public String getHost() {
  418.     return host;
  419.     }
  420.  
  421.     /**
  422.      * Returns the file name of this <code>URL</code>.
  423.      *
  424.      * @return  the file name of this <code>URL</code>.
  425.      * @since   JDK1.0
  426.      */
  427.     public String getFile() {
  428.     return file;
  429.     }
  430.  
  431.     /**
  432.      * Returns the anchor (also known as the "reference") of this
  433.      * <code>URL</code>.
  434.      *
  435.      * @return  the anchor (also known as the "reference") of this
  436.      *          <code>URL</code>.
  437.      * @since   JDK1.0
  438.      */
  439.     public String getRef() {
  440.     return ref;
  441.     }
  442.  
  443.     /**
  444.      * Compares two URLs.
  445.      * The result is <code>true</code> if and only if the argument is 
  446.      * not <code>null</code> and is a <code>URL</code> object that 
  447.      * represents the same <code>URL</code> as this object. Two URL 
  448.      * objects are equal if they have the same protocol and reference the 
  449.      * same host, the same port number on the host, and the same file on 
  450.      * the host. The anchors of the URL objects are not compared. 
  451.      * <p>
  452.      * This method is equivalent to:
  453.      * <ul><code>
  454.      *     (obj instanceof URL) && sameFile((URL)obj)
  455.      * </code></ul>
  456.      *
  457.      * @param   obj   the URL to compare against.
  458.      * @return  <code>true</code> if the objects are the same;
  459.      *          <code>false</code> otherwise.
  460.      * @since   JDK1.0
  461.      */
  462.     public boolean equals(Object obj) {
  463.     return (ref == null) ? (obj instanceof URL) && sameFile((URL)obj):
  464.         (obj instanceof URL) && sameFile((URL)obj) && ref.equals(((URL)obj).ref);
  465.     }
  466.  
  467.     /** 
  468.      * Creates an integer suitable for hash table indexing. 
  469.      *
  470.      * @return  a hash code for this <code>URL</code>.
  471.      * @since   JDK1.0
  472.      */
  473.     public int hashCode() {
  474.  
  475.     if (hashCode == -1) {
  476.         hashCode = host.toLowerCase().hashCode() ^ file.hashCode() ^
  477.         protocol.hashCode();
  478.     }
  479.  
  480.     return hashCode;
  481.     }
  482.     
  483.     /**
  484.      * Compares the host components of two URLs.
  485.      * @param h1 the URL of the first host to compare 
  486.      * @param h2 the URL of the second host to compare 
  487.      * @return    true if and only if they are equal, false otherwise.
  488.      * @exception UnknownHostException If an unknown host is found.
  489.      */
  490.     boolean hostsEqual(String h1, String h2) {
  491.     if (h1.equals(h2)) {
  492.         return true;
  493.     }
  494.     // Have to resolve addresses before comparing, otherwise
  495.     // names like tachyon and tachyon.eng would compare different
  496.     try {
  497.         InetAddress a1 = InetAddress.getByName(h1);
  498.         InetAddress a2 = InetAddress.getByName(h2);
  499.         return a1.equals(a2);
  500.     } catch(UnknownHostException e) {
  501.     } catch(SecurityException e) {
  502.     }
  503.     return false;
  504.     }
  505.  
  506.     /**
  507.      * Compares two URLs, excluding the "ref" fields.
  508.      * Returns <code>true</code> if this <code>URL</code> and the 
  509.      * <code>other</code> argument both refer to the same resource.
  510.      * The two <code>URL</code>s might not both contain the same anchor. 
  511.      *
  512.      * @param   other   the <code>URL</code> to compare against.
  513.      * @return  <code>true</code> if they reference the same remote object;
  514.      *          <code>false</code> otherwise.
  515.      * @since   JDK1.0
  516.      */
  517.     public boolean sameFile(URL other) {
  518.     // AVH: should we not user getPort to compare ports?
  519.     return protocol.equals(other.protocol) &&
  520.            hostsEqual(host, other.host) &&
  521.            (port == other.port) &&
  522.            file.equals(other.file);
  523.     }
  524.  
  525.     /**
  526.      * Constructs a string representation of this <code>URL</code>. The 
  527.      * string is created by calling the <code>toExternalForm</code> 
  528.      * method of the stream protocol handler for this object. 
  529.      *
  530.      * @return  a string representation of this object.
  531.      * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  532.      * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
  533.      * @since   JDK1.0
  534.      */
  535.     public String toString() {
  536.     return toExternalForm();
  537.     }
  538.  
  539.     /**
  540.      * Constructs a string representation of this <code>URL</code>. The 
  541.      * string is created by calling the <code>toExternalForm</code> 
  542.      * method of the stream protocol handler for this object. 
  543.      *
  544.      * @return  a string representation of this object.
  545.      * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  546.      * @see     java.net.URLStreamHandler#toExternalForm(java.net.URL)
  547.      * @since   JDK1.0
  548.      */
  549.     public String toExternalForm() {
  550.     return handler.toExternalForm(this);
  551.     }
  552.  
  553.     /** 
  554.      * Returns a <code>URLConnection</code> object that represents a 
  555.      * connection to the remote object referred to by the <code>URL</code>.
  556.      * <p>
  557.      * If there is not already an open connection, the connection is 
  558.      * opened by calling the <code>openConnection</code> method of the 
  559.      * protocol handler for this URL. 
  560.      *
  561.      * @return     a <code>URLConnection</code> to the URL.
  562.      * @exception  IOException  if an I/O exception occurs.
  563.      * @see        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  564.      * @see        java.net.URLConnection
  565.      * @see        java.net.URLStreamHandler#openConnection(java.net.URL)
  566.      * @since      JDK1.0
  567.      */
  568.     public URLConnection openConnection()
  569.     throws java.io.IOException
  570.     {
  571.     return handler.openConnection(this);
  572.     }
  573.  
  574.     /**
  575.      * Opens a connection to this <code>URL</code> and returns an 
  576.      * <code>InputStream</code> for reading from that connection. This 
  577.      * method is a shorthand for:
  578.      * <ul><code>
  579.      *     openConnection().getInputStream()
  580.      * </code></ul>
  581.      *
  582.      * @return     an input stream for reading from the URL connection.
  583.      * @exception  IOException  if an I/O exception occurs.
  584.      * @since      JDK1.0
  585.      */
  586.     public final InputStream openStream()             // REMIND: drop final
  587.     throws java.io.IOException
  588.     {
  589.     return openConnection().getInputStream();
  590.     }
  591.  
  592.     /**
  593.      * Returns the contents of this URL. This method is a shorthand for:
  594.      * <ul><code>
  595.      *     openConnection().getContent()
  596.      * </code></ul>
  597.      *
  598.      * @return     the contents of this URL.
  599.      * @exception  IOException  if an I/O exception occurs.
  600.      * @see        java.net.URLConnection#getContent()
  601.      * @since      JDK1.0
  602.      */
  603.     public final Object getContent()                 // REMIND: drop final
  604.     throws java.io.IOException
  605.     {
  606.     return openConnection().getContent();
  607.     }
  608.  
  609.     /**
  610.      * The URLStreamHandler factory.
  611.      */
  612.     static URLStreamHandlerFactory factory;
  613.  
  614.     /**
  615.      * Sets an application's <code>URLStreamHandlerFactory</code>.
  616.      * This method can be called at most once by an application. 
  617.      * <p>
  618.      * The <code>URLStreamHandlerFactory</code> instance is used to 
  619.      * construct a stream protocol handler from a protocol name. 
  620.      *
  621.      * @param      fac   the desired factory.
  622.      * @exception  Error  if the application has already set a factory.
  623.      * @see        java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
  624.      * @see        java.net.URLStreamHandlerFactory
  625.      * @since      JDK1.0
  626.      */
  627.     public static synchronized void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) {
  628.     if (factory != null) {
  629.         throw new Error("factory already defined");
  630.     }
  631.     SecurityManager security = System.getSecurityManager();
  632.     if (security != null) {
  633.         security.checkSetFactory();
  634.     }
  635.     handlers.clear();
  636.     factory = fac;
  637.     }
  638.  
  639.     /**
  640.      * A table of protocol handlers.
  641.      */
  642.     static Hashtable handlers = new Hashtable();
  643.  
  644.     /**
  645.      * Returns the Stream Handler.
  646.      * @param protocol the protocol to use
  647.      */
  648.     static synchronized URLStreamHandler getURLStreamHandler(String protocol) {
  649.     URLStreamHandler handler = (URLStreamHandler)handlers.get(protocol);
  650.     if (handler == null) {
  651.         // Use the factory (if any)
  652.         if (factory != null) {
  653.         handler = factory.createURLStreamHandler(protocol);
  654.         }
  655.  
  656.         // Try java protocol handler
  657.         if (handler == null) {
  658.         String packagePrefixList =
  659.             System.getProperty(protocolPathProp, "");
  660.         if (packagePrefixList != "") {
  661.             packagePrefixList += "|";
  662.         }
  663.  
  664.         // REMIND: decide whether to allow the "null" class prefix
  665.         // or not.
  666.         packagePrefixList += "sun.net.www.protocol";
  667.  
  668.         StringTokenizer packagePrefixIter =
  669.             new StringTokenizer(packagePrefixList, "|");
  670.  
  671.         while (handler == null && packagePrefixIter.hasMoreTokens()) {
  672.             String packagePrefix = packagePrefixIter.nextToken().trim();
  673.             try {
  674.             String clname = packagePrefix + "." + protocol
  675.                 + ".Handler";
  676.             handler = (URLStreamHandler)Class.forName(clname).newInstance();
  677.             } catch (Exception e) {
  678.             }
  679.         }
  680.         }
  681.         if (handler != null) {
  682.         handlers.put(protocol, handler);
  683.         }
  684.     }
  685.     return handler;
  686.     }
  687.  
  688.     /**
  689.      * WriteObject is called to save the state of the URL to an ObjectOutputStream
  690.      * The handler is not saved since it is specific to this system.
  691.      */
  692.     private synchronized void writeObject(java.io.ObjectOutputStream s)
  693.         throws IOException
  694.     {
  695.     s.defaultWriteObject();    // write the fields
  696.     }
  697.  
  698.     /**
  699.      * readObject is called to restore the state of the URL from the stream.
  700.      * It reads the compoents of the URL and finds the local stream handler.
  701.      */
  702.     private synchronized void readObject(java.io.ObjectInputStream s)
  703.          throws IOException, ClassNotFoundException
  704.     {
  705.     s.defaultReadObject();    // read the fields
  706.     if ((handler = getURLStreamHandler(protocol)) == null) {
  707.         throw new IOException("unknown protocol: " + protocol);
  708.     }
  709.     }
  710. }
  711.